﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using WmsApp.Service;
using WmsApp.StaticData.Unity;
using WmsApp.WmsService;

namespace WmsApp.Window
{
    public partial class FormInvoiceSelector : DevExpress.XtraEditors.XtraForm,ICanClosing
    {
        private string providerCode = string.Empty;
        private const string ConditionPayApplyBill =
            " and ProviderCode ='{0}' and AmountTax= InStoreBillAmount and  dbo.GetPayApplyBillDtlSumAmount(InvoiceNo) < AmountTax";

        public FormInvoiceSelector(string providerCode)
        {
            InitializeComponent();
            this.providerCode = providerCode;
            this.gridControlEdit.DataSource = new List<ExtendInvoiceBill>();
            FExtendInvoiceBill = new List<ExtendInvoiceBill>();
        }

        public List<ExtendInvoiceBill> FExtendInvoiceBill { get; private set; }

        private void CmdQuery_Click(object sender, EventArgs e)
        {
            var service = EntityServiceFactory.GetEntityService<ExtendInvoiceBill, ExtendInvoiceBillService>();
            var list  = service.GetEntityList(this.qInvoiceBill1.GetCondition() +
                          String.Format(ConditionPayApplyBill,providerCode));
            CopyAmountTaxToNeedPayAmount(list);
            this.gridControlEdit.DataSource = list;
        
        }

        private void CopyAmountTaxToNeedPayAmount(List<ExtendInvoiceBill> list)
        {
            foreach(var entity in list)
            {
                entity.PayApplyBillDtlPayAmountTax = entity.AmountTax - entity.PayCompleteAmount;
            }
        }

        private void CmdSelect_Click(object sender, EventArgs e)
        {
            var list = (List<ExtendInvoiceBill>)this.gridControlEdit.DataSource;
            foreach (var extendInvoiceBill in list)
            {
                if (extendInvoiceBill.IsSelect)
                {
                    //条件，选中，付款单为空，金额等于已经瓜分的金额
                    if (/*String.IsNullOrEmpty(extendInvoiceBill.PayApplyBillID) &&*/
                        extendInvoiceBill.AmountTax == extendInvoiceBill.InStoreBillAmount)
                    {
                        FExtendInvoiceBill.Add(extendInvoiceBill);
                    }
                    else
                    {
                        XtraMessageBox.Show("只有未付款且金额等于已经分配金额的发票才能加入付款申请单！", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        return;
                    }
                }
            }
            if(FExtendInvoiceBill.Count>0)
            {
                this.DialogResult = DialogResult.OK;
            }
            else
            {
                XtraMessageBox.Show("请选择要加入付款申请单的发票！", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }
    }
}